package com.lnkj.spcj.ui.main

import android.content.Context
import androidx.fragment.app.Fragment
import androidx.viewpager.widget.ViewPager
import com.lnkj.spcj.MyApplication
import com.lnkj.spcj.R
import com.lnkj.spcj.base.BaseActivity
import com.lnkj.spcj.ui.demo.DemoFragment
import com.lnkj.spcj.util.EventBusUtils
import com.lnkj.spcj.util.utilcode.BarUtils
import com.lnkj.spcj.util.utilcode.ToastUtils
import com.lnkj.spcj.widget.CustomFragmentPagerAdapter
import com.lnkj.spcj.widget.NativeTabButton
import kotlinx.android.synthetic.main.activity_main.*
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode

class MainActivity : BaseActivity<MainContract.Present>(), MainContract.View {

    override val mPresenter: MainContract.Present
        get() = MainPresent().also { it.attachView(this) }
    override val layoutRes: Int
        get() = R.layout.activity_main
    private var mTabButtons: Array<NativeTabButton>? = null
    private var mFragments: Array<Fragment>? = null
    private var title: Array<String>? = null
    private var checkImage = intArrayOf(R.drawable.tabbar_icon_home_s, R.drawable.tabbar_icon_rx_s, R.drawable.tabbar_icon_hxx_s, R.drawable.tabbar_icon_xd_s, R.drawable.tabbar_icon_me_s)
    private var unCheckImage = intArrayOf(R.drawable.tabbar_icon_home_n, R.drawable.tabbar_icon_rx_n, R.drawable.tabbar_icon_hxx_n, R.drawable.tabbar_icon_xd_n, R.drawable.tabbar_icon_me_n)
    private var adapter: CustomFragmentPagerAdapter? = null

    override fun initAll() {
        EventBusUtils.register(this)
        BarUtils.setStatusBarAlpha(this, 0)
        BarUtils.setAndroidNativeLightStatusBar(this, true)
        title = arrayOf(getString(R.string.main_tab0), getString(R.string.main_tab1), getString(R.string.main_tab2), getString(R.string.main_tab3), getString(R.string.main_tab4))
        initTab()
        initFragment()
        setFragmentShow(0)
    }

    override fun onDestroy() {
        super.onDestroy()
        EventBusUtils.unregister(this)
    }

    override fun setListener() {

    }

    override fun processLogic() {

    }

    override fun getContext(): Context {
        return mContext
    }

    override fun onEmpty() {

    }

    override fun onError() {

    }

    fun initTab() {
        mTabButtons = arrayOf(tab0, tab1, tab2, tab3, tab4)
        for (i in mTabButtons!!.indices) {
            mTabButtons!![i].setTitle(title!![i])
            mTabButtons!![i].setIndex(i)
            mTabButtons!![i].setSelectedImage(resources.getDrawable(checkImage[i]))
            mTabButtons!![i].setUnselectedImage(resources.getDrawable(unCheckImage[i]))
        }
    }

    fun initFragment() {
        mFragments = arrayOf(DemoFragment.newInstance(null), DemoFragment.newInstance(null), DemoFragment.newInstance(null), DemoFragment.newInstance(null), DemoFragment.newInstance(null))
        adapter = CustomFragmentPagerAdapter(mFragments, supportFragmentManager)
        sp_main.adapter = adapter
        sp_main.offscreenPageLimit = 5
        sp_main.setScroll(true)
        sp_main.addOnPageChangeListener(object : ViewPager.OnPageChangeListener {
            override fun onPageScrollStateChanged(state: Int) {

            }

            override fun onPageScrolled(position: Int, positionOffset: Float, positionOffsetPixels: Int) {

            }

            override fun onPageSelected(position: Int) {
                for (item in mTabButtons!!) {
                    item.setSelectedButton(false)
                }
                mTabButtons!![position].setSelectedButton(true)
            }
        })
    }

    fun setFragmentShow(index: Int) {
        sp_main.setCurrentItem(index, false)
        for (item in mTabButtons!!) {
            item.setSelectedButton(false)
        }
        mTabButtons!![index].setSelectedButton(true)
    }

    private var lastTime: Long = 0

    override fun onBackPressed() {
        if (System.currentTimeMillis() - lastTime < 3000) {
            super.onBackPressed()
        } else {
            lastTime = System.currentTimeMillis()
            ToastUtils.showShort("再按一次退出")
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    fun onReceiveEvent(event: EventBusUtils.EventMessage<*>) {
        when (event.code) {
            EventBusUtils.EventCode.EVENT_RELOGIN -> {
                MyApplication.getInstance().reLogin()
            }
        }
    }
}
